<template>
  <div :class="className" :style="{height:height,width:width}"></div>
</template>

<script>
  import echarts from 'echarts'
  require('echarts/theme/macarons') // echarts theme
  import resize from './mixins/resize'

  const animationDuration = 6000

  export default {
    mixins: [resize],
    props: {
      className: {
        type: String,
        default: 'chart'
      },
      width: {
        type: String,
        default: '100%'
      },
      height: {
        type: String,
        default: '100%'
      }
    },
    data() {
      return {
        chart: null
      }
    },
    mounted() {
      this.$nextTick(() => {
        this.initChart()
      })
    },
    beforeDestroy() {
      if (!this.chart) {
        return
      }
      this.chart.dispose()
      this.chart = null
    },
    methods: {
      initChart() {
        this.chart = echarts.init(this.$el, 'macarons')

        this.chart.setOption({
          title: {
            text: '数据接入情况统计',
            textStyle: {
              color: 'yellow'
            },
            x: '8%',
            y: '8%'
          },
          tooltip: {
            trigger: 'axis',
            axisPointer: { // 坐标轴指示器，坐标轴触发有效
              type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
            }
          },
          legend: {
            data: ['单位数量', '设备数量', '报告数量'],
            textStyle: {
              color: 'white'
            }
          },
          grid: {
            top: '25%',
            left: '20%',
            containLabel: true
          },
          xAxis: [{
            type: 'category',
            data: ['山东省', '安徽省', '河北省', '湖北省'],
            textStyle: {
              color: 'white'
            },
            axisTick: {
              show: false
            }
          }],
          yAxis: [{
            type: 'value',
            scale : true,
            max : 40000,
            min : 0,
            splitNumber : 4,
            axisTick: {
              show: false
            }
          }],
          series: [{
            name: '单位数量',
            type: 'bar',
            barWidth: '30%',
            barGap: '0%',
            itemStyle: {
              normal: {
                color: function(params) {
                  var colorList = [
                    '#C1232B'
                  ]
                  return colorList[params.dataIndex]
                },
                label: {
                  show: true,
                  position: 'top',
                  formatter: '{b}\n{c}'
                }
              }
            },
            data: [3224, 438, 1, 1]
          }, {
            name: '设备数量',
            type: 'bar',
            barWidth: '30%',
            itemStyle: {
              normal: {
                color: function(params) {
                  var colorList = [
                    '#E87C25'
                  ]
                  return colorList[params.dataIndex]
                },
                label: {
                  show: true,
                  position: 'top',
                  formatter: '{b}\n{c}'
                }
              }
            },
            data: [20584, 3070, 10, 2]
          }, {
            name: '报告数量',
            type: 'bar',
            barWidth: '30%',
            itemStyle: {
              normal: {
                color: function(params) {
                  var colorList = [
                    '#26C0C0'
                  ]
                  return colorList[params.dataIndex]
                },
                label: {
                  show: true,
                  position: 'top',
                  formatter: '{b}\n{c}'
                }
              }
            },
            data: [28695, 2776, 20, 4]
          }]
        })
      }
    }
  }
</script>
